//
//  ContactController.m
//  Ghac 4S
//
//  Created by apple on 11-8-29.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//
#import "Global.h"
#import "ContactController.h"
#import "AccordionNavigator.h"
#import "AccordionTitleView.h"
#import "MainEntry.h"

@interface UIColor(ghac)
+(id) colorWithRGB:(NSUInteger)rgb alpha:(CGFloat)alpha;
@end

@implementation UIColor(ghac)

+(id) colorWithRGB:(NSUInteger)rgb alpha:(CGFloat)alpha
{
    CGFloat r = rgb >> 16 & 0x0000ff;
    CGFloat g = rgb >> 8 & 0x0000ff;
    CGFloat b = rgb & 0x0000ff;
    return [UIColor colorWithRed:r / 0xff green:g / 0xff blue:b / 0xff alpha:alpha];
}
@end


@implementation ContactController
@synthesize mainNavigation;
@synthesize customServiceView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
    }
    return self;
}
-(void)dealloc
{
    [super dealloc];
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    [self.view addSubview:[MyNavigationController mainNavigationBar].view];
    
    //fot the config file
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSDictionary* config = [Global globalConfigDic];
    
    
    //set for the ins_table;
    UIView* insuContactTable = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 304, 20)];
    
    NSArray* array = [[config objectForKey:@"Contact"] objectForKey:@"insu"];
    NSUInteger insu_count =  [array count];
    CGFloat tmpHeight = 0;
    const CGFloat BTN_HEIGHT = 72;
    for (NSInteger i = 0; i < insu_count; i++)
    {
        UIImage* normalImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[[array objectAtIndex:i] objectForKey:@"pic0"] ofType:@"png"]];
        UIImage* highLighImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[[array objectAtIndex:i] objectForKey:@"pic1"] ofType:@"png"]];
        
        tmpHeight += BTN_HEIGHT;
        UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, i * BTN_HEIGHT, 304, BTN_HEIGHT)];
        [button setImage:normalImage forState:UIControlStateNormal];
        [button setImage:highLighImage forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(insuClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:i];
        [insuContactTable addSubview:button];
        CGRect oldFrame = insuContactTable.frame;
        oldFrame.size.height = tmpHeight;
        [insuContactTable setFrame:oldFrame];
        [button release];
    }
    
    //set for the navigator
    
    CGRect mainFrame = self.view.frame;
    NSArray* titleImageUrlList = [[NSArray alloc] initWithObjects:@"tel_06", @"tel_03", @"tel_06", @"tel_03", nil];
    NSArray* pageViewList = [[NSArray alloc] initWithObjects:self.customServiceView, insuContactTable, nil];
   [mainNavigation initWithFrame:mainNavigation.frame titleFrameSize:CGSizeMake(mainNavigation.frame.size.width, 31)];

    UIColor* pageBgColor = [UIColor colorWithRGB:0x2c464c alpha:0.7];
    [mainNavigation setBackgroundColor:pageBgColor];
    
    for (NSInteger i = 0; i < titleImageUrlList.count / 2; i++)
    {
        AccordionTitleView* titleView = [[AccordionTitleView alloc] init];
        UIImage* normalImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[titleImageUrlList objectAtIndex:i * 2] ofType:@"png" ]];
        UIImage* highLighImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[titleImageUrlList objectAtIndex:i * 2 + 1] ofType:@"png" ]];
        
        
        [titleView setImage:normalImage forState:UIControlStateNormal];
        [titleView setImage:highLighImage forState:UIControlStateSelected];
        
        UIView* pageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, mainNavigation.frameSize.width, 1000)];
        
        if (pageViewList.count > i) 
        {
            CGRect frame = pageView.frame;
            UIView* pageContentView = [pageViewList objectAtIndex:i];
            frame.size.height = pageContentView.frame.size.height;
            [pageView setFrame:frame];
            [pageContentView setFrame:CGRectMake((mainNavigation.frame.size.width - pageContentView.frame.size.width) / 2, 10, pageContentView.frame.size.width, pageContentView.frame.size.height)];
            [pageView addSubview:pageContentView];
//            NSLog(@"a %f, %f", titleView.frame.size.width , pageContentView.frame.size.width);
        }

        [mainNavigation addPageView:pageView withTitleView:titleView];
        
        [titleView release];
        [pageView release];
    }
    [pool drain];
    [insuContactTable release];
    [titleImageUrlList release];
    [self.view addSubview:mainNavigation];
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([actionSheet.title isEqualToString:@"保险公司号码"])
    {
        if (buttonIndex == 0)
        {
            NSDictionary* config = [Global globalConfigDic];
            NSArray* array = [[config objectForKey:@"Contact"] objectForKey:@"insu"];
            NSString* telStr = [[array objectAtIndex:[actionSheet tag]] objectForKey:@"tel"];
            [Global phoneCall:telStr];
        }
    }
    else if ([actionSheet.title isEqualToString:@"客服号码"])
    {
        if (buttonIndex == 0)
        {
            NSDictionary* config = [Global globalConfigDic];
            NSArray* array = [[config objectForKey:@"Contact"] objectForKey:@"custom"];
            NSString* telStr = [array objectAtIndex:[actionSheet tag]];
            [Global phoneCall:telStr];
        }
    }
    
}
-(void) insuClick:(id)sender
{
    
    NSDictionary* config = [Global globalConfigDic];
    NSArray* array = [[config objectForKey:@"Contact"] objectForKey:@"insu"];
    NSString* telNum = [[array objectAtIndex:[sender tag]] objectForKey:@"tel"];
    NSString* telStr = [[NSString alloc] initWithFormat:@"拨打%@", telNum];
    UIActionSheet* myActionSheet = [[UIActionSheet alloc] initWithTitle:@"保险公司号码" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:telStr, nil];
    UIWindow* window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    [myActionSheet showInView:window.rootViewController.view];
    [myActionSheet setTag:[sender tag]];
    [telStr release];
    [myActionSheet release];
}
-(void)customServiceCallClick:(id)sender
{
    NSDictionary* config = [Global globalConfigDic];
    NSArray* array = [[config objectForKey:@"Contact"] objectForKey:@"custom"];
    NSString* telNum = [array objectAtIndex:[sender tag]];
    NSString* telStr = [[NSString alloc] initWithFormat:@"拨打%@", telNum];
    UIActionSheet* myActionSheet = [[UIActionSheet alloc] initWithTitle:@"客服号码" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:telStr, nil];
    UIWindow* window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    [myActionSheet showInView:window.rootViewController.view];
    [myActionSheet setTag:[sender tag]];
    [telStr release];
    [myActionSheet release];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

